package org.infodavid.common.impl.persistence;

import javax.persistence.PersistenceException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.infodavid.common.IBuilder;
import org.infodavid.common.dto.IOrganizationListItem;
import org.infodavid.common.model.IOrganization;
import org.infodavid.common.model.ModelFactory;
import org.infodavid.common.persistence.OrganizationDataService;
import org.infodavid.common.persistence.PersonDataService;

/**
 * The Class AbstractOrganizationDataService.
 */
public abstract class AbstractOrganizationDataService extends OrganizationDataService {

	/** The Constant log. */
	private static final Log LOGGER = LogFactory.getLog(AbstractOrganizationDataService.class);

	/** The builder. */
	private final IBuilder<IOrganization> builder = ModelFactory.getInstance().getBuilder(
	    IOrganization.class);

	/**
	 * The Constructor.
	 */
	protected AbstractOrganizationDataService() {
		super();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.persistence.IDataService#create()
	 */
	public IOrganization create() throws InstantiationException {
		return builder.create();
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see org.infodavid.common.persistence.IDataService#copyTo(org.infodavid.common.model.IDataObject,
	 * boolean, boolean)
	 */
	public IOrganization copyTo(final IOrganization from, final boolean deep, final boolean asNew)
	    throws PersistenceException, InstantiationException {
		final boolean isDebugEnabled = LOGGER.isDebugEnabled();

		if (isDebugEnabled) {
			LOGGER.debug("Copying entity from " + from + " using deep = " + deep + ", asNew = " + asNew);
		}

		final IOrganization to;

		if (from == null) {
			to = null;
		}
		else {
			to = builder.create();

			copyTo(from, to, asNew);
			to.setComment(from.getComment());
			to.setName(from.getName());
			to.setIdentity(from.getIdentity());
			to.setImage(from.getImage());
			to.setDirector(PersonDataService.getInstance()
			    .copyListItemTo(from.getDirector(), deep, asNew));
			to.setRegistrationDate(from.getRegistrationDate());
			to.setBusinessType(from.getBusinessType());
			to.setLegalForm(from.getLegalForm());
			to.setMainSite(copyListItemTo(from.getMainSite(), deep, asNew));
			CopyUtil.copyEmails(from.getEmails(), to.getEmails(), deep);
			CopyUtil.copyLocations(from.getLocations(), to.getLocations(), deep, asNew);
			CopyUtil.copyPhones(from.getPhones(), to.getPhones(), deep);
			CopyUtil.copyUris(from.getWebSites(), to.getWebSites(), deep);
			to.getProperties().getData().addAll(from.getProperties().getData());

			if (from.getContacts() == null) {}
			else {
				to.getContacts().putAll(from.getContacts());
			}
		}

		if (isDebugEnabled) {
			LOGGER.debug("Copied entity = " + to);
		}

		return to;
	}

	/*
	 * (non-Javadoc)
	 * @see
	 * org.infodavid.common.persistence.IDataService#copyListItemTo(org.infodavid.common.model.IDataObject
	 * , boolean, boolean)
	 */
	public IOrganizationListItem copyListItemTo(final IOrganizationListItem from, final boolean deep,
	    final boolean asNew) throws PersistenceException, InstantiationException {
		final boolean isDebugEnabled = LOGGER.isDebugEnabled();

		if (isDebugEnabled) {
			LOGGER.debug("Copying entity from " + from + " using deep = " + deep + ", asNew = " + asNew);
		}

		final IOrganization to;

		if (from == null) {
			to = null;
		}
		else {
			to = builder.create();

			copyTo(from, to, asNew);
			to.setName(from.getName());
			to.setIdentity(from.getIdentity());
			to.setRegistrationDate(from.getRegistrationDate());
			to.setBusinessType(from.getBusinessType());
			to.setLegalForm(from.getLegalForm());
		}

		if (isDebugEnabled) {
			LOGGER.debug("Copied entity = " + to);
		}

		return to;
	}
}
